#include "QStyle"
#include <QLabel>
#include <QTimer>
#include <QKeyEvent>
#include "combatscene.h"
CombatScene::CombatScene()
{
    world=Factory::createWorld();
    contactSignalSender=new ContactSignal();
    world->SetContactListener(new ContactController(contactSignalSender));
    raycastController=new RayCastController();
    num=60;
    bgm=new QMediaPlayer();
    //人物
    p1=new Player("p1");
    p2=new Player("p2");
    p1->setAgainstPlayer(p2);
    p2->setAgainstPlayer(p1);
    aien=new AIEntry(p2);
    scoreLabel1=new QLabel;
    scoreLabel2=new QLabel;
    //头像
    hLabel1=new QLabel;
    hLabel2=new QLabel;

    pointLabel6=new QLabel;
    pointLabel5=new QLabel;
    pointLabel4=new QLabel;
    pointLabel3=new QLabel;
    pointLabel2=new QLabel;
    pointLabel1=new QLabel;

    skillLabel2=new QLabel;
    skillLabel=new QLabel;

    mask1=new ShadowFanMask(10,90,55,55,0*16,0*16);
    mask2=new ShadowFanMask(sceneRect().width()-60,90,55,55,0*16,0*16);

    //控制遮罩 变化
//    scoreTimer=new QTimer();
//    connect(scoreTimer,&QTimer::timeout,[&](){
//        if(score1!=0){
//            score1--;
//        }else{
//            score1=150;
//        }

//        if(score2!=0){
//            score2--;
//        }else{
//            score2=150;
//        }
//    });



    timerLabel=new QLabel;
    timer=new QTimer(this);
    p2->setBody(Factory::createPlayerBody(world,"p2",90.0f,4.0f));
    p1->setBody(Factory::createPlayerBody(world,"p1",3.0f,4.0f));
    connect(contactSignalSender,&ContactSignal::isOnFloor,this,&CombatScene::playerInFloor);
    connect(contactSignalSender,&ContactSignal::attacked,this,&CombatScene::playerIsAttacked);
    connect(contactSignalSender,&ContactSignal::combinationAttacked,this,&CombatScene::playerIsCombinationAttacked);


    connect(timer,&QTimer::timeout,[=]()mutable{
        timerLabel->setText(QString::number(num--));
        if(num==0){
            MainWindow *mainWindow=(MainWindow *)(getView()->parent()->parent());
            mainWindow->scoreP1=p1->getScore();
            mainWindow->scoreP2=p2->getScore();

            mainWindow->setScene(mainWindow->overScene);
            timerLabel->setText("");   //将标签中置空

            bgm->stop();
            timer->stop();
        }
    });


}


void CombatScene::init(){

    bgm->setMedia(QUrl("qrc:/bg.wav"));
    bgm->play();
    //计时器
    num=60;
    timer->start(1000);

    timerLabel->setStyleSheet("background:transparent");    // 边框设置为透明
    QFont ft;
    ft.setPointSize(30);
    timerLabel->setFont(ft);     //设置 字体大小
    timerLabel->setGeometry(sceneRect().width()/2,20,100,50);
    addWidget(timerLabel);

    p1->setStatus(jumpingRight);
    p1->setRayCast(raycastController);
    p2->setRayCast(raycastController);
    p1->init();
    qDebug()<<"我运行到了这里"<<endl;
    addItem(p1->getGraphics());

    p2->setStatus(jumpingLeft);
    p2->init();
    addItem(p2->getGraphics());

    p1->getBody()->SetTransform(b2Vec2(5.0f,-40.0f),0);
    p2->getBody()->SetTransform(b2Vec2(85.0f,-40.0f),0);

    scoreLabel1->setStyleSheet("background:transparent");
    scoreLabel2->setStyleSheet("background:transparent");
    scoreLabel1->setFont(ft);     //设置 字体大小
    scoreLabel2->setFont(ft);
    scoreLabel1->setGeometry(100,20,100,50);
    scoreLabel2->setGeometry(sceneRect().width()-150,20,100,50);


    qDebug()<<p1->getScore();
    scoreLabel1->setText(QString::number(p1->getScore()));
    scoreLabel2->setText(QString::number(p2->getScore()));

    addWidget(scoreLabel1);
    addWidget(scoreLabel2);



    //头像
//    QLabel * hLabel1=new QLabel;
//    QLabel * hLabel2=new QLabel;
//    hLabel1->setStyleSheet("background:transparent");
//    hLabel2->setStyleSheet("background:transparent");

//    QGraphicsPixmapItem * p1Photo=new QGraphicsPixmapItem;
//    QGraphicsPixmapItem * p2Photo=new QGraphicsPixmapItem;
//    p1Photo->setPixmap(QPixmap(":/stand.png"));
//    p2Photo->setPixmap(QPixmap(":/stand.png"));

    hLabel1->setGeometry(5,10,70,70);
    hLabel2->setGeometry(sceneRect().width()-70,10,70,70);
    hLabel1->setPixmap(QPixmap(":/p1HPhoto.jpg"));
    hLabel2->setPixmap(QPixmap(":/p2HPhoto.jpg"));

    addWidget(hLabel1);
    addWidget(hLabel2);

    //技能

    //技能图标

    skillLabel->setGeometry(10,90,55,55);
    skillLabel->setStyleSheet("background:transparent");
    skillLabel->setPixmap(QPixmap(":/p1Skill.png"));

//    QString test = "QLabel{border-radius: 15px;border:1px solid red; background:black;}";
//    QString test2="border-radius: 15px;border:1px solid black;background-image: url(:/skillRound.png) ;";
//    skillLabel->setStyleSheet(test2);

    skillLabel2->setGeometry(sceneRect().width()-60,90,55,55);
    skillLabel2->setStyleSheet("background:transparent");
    skillLabel2->setPixmap(QPixmap(":/p2Skill.png"));

    addWidget(skillLabel);
    addWidget(skillLabel2);


    //技能点

      pointLabel1->setGeometry(75,90,55,55);
      pointLabel1->setStyleSheet("background:transparent");
      pointLabel1->setPixmap(QPixmap(":/point2.png"));


      pointLabel2->setGeometry(110,90,55,55);
      pointLabel2->setStyleSheet("background:transparent");
      pointLabel2->setPixmap(QPixmap(":/point1.png"));


      pointLabel3->setGeometry(145,90,55,55);
      pointLabel3->setStyleSheet("background:transparent");
      pointLabel3->setPixmap(QPixmap(":/point1.png"));


      pointLabel4->setGeometry(sceneRect().width()-90,90,55,55);
      pointLabel4->setStyleSheet("background:transparent");
      pointLabel4->setPixmap(QPixmap(":/point2.png"));


      pointLabel5->setGeometry(sceneRect().width()-125,90,55,55);
      pointLabel5->setStyleSheet("background:transparent");
      pointLabel5->setPixmap(QPixmap(":/point1.png"));


      pointLabel6->setGeometry(sceneRect().width()-160,90,55,55);
      pointLabel6->setStyleSheet("background:transparent");
      pointLabel6->setPixmap(QPixmap(":/point1.png"));

    p1->setpointLabel1(pointLabel1);
    p1->setpointLabel2(pointLabel2);
    p1->setpointLabel3(pointLabel3);

    p2->setpointLabel1(pointLabel4);
    p2->setpointLabel2(pointLabel5);
    p2->setpointLabel3(pointLabel6);

    p1->getpointLabel1()->setGeometry(75,90,55,55);
    p1->getpointLabel2()->setGeometry(110,90,55,55);
    p1->getpointLabel3()->setGeometry(145,90,55,55);

    p2->getpointLabel1()->setGeometry(sceneRect().width()-90,90,55,55);
    p2->getpointLabel2()->setGeometry(sceneRect().width()-125,90,55,55);
    p2->getpointLabel3()->setGeometry(sceneRect().width()-160,90,55,55);


//    addWidget(pointLabel1);
//    addWidget(pointLabel2);
//    addWidget(pointLabel3);
//    addWidget(pointLabel4);
//    addWidget(pointLabel5);
//    addWidget(pointLabel6);

    addWidget(p1->getpointLabel1());
    addWidget(p1->getpointLabel2());
    addWidget(p1->getpointLabel3());

    addWidget(p2->getpointLabel1());
    addWidget(p2->getpointLabel2());
    addWidget(p2->getpointLabel3());


    //控制遮罩的变化

//    scoreTimer->start(15000/360);


    addItem(mask1);
    addItem(mask2);

    qDebug()<<"CombatScene_init";
    connect(contactSignalSender,&ContactSignal::isOnFloor,this,&CombatScene::playerInFloor);
}




void CombatScene::run(){
    //1秒更新60次
    world->Step(1.0f/60,6,2);
    p1->run();
    p2->run();

    //遮罩的更新
    update();
    if(p1->getCombinationAttackCD()==0){
        mask1->setAngle(0,0*16);
    }else{
        score1=p1->getCombinationAttackCD();

        mask1->setAngle(0,(score1/150.0)*360*16);
        qDebug()<<"score1"<<score1;
    }

    if(p2->getCombinationAttackCD()==0){
        mask2->setAngle(0,0*16);
    }else{
        score2=p2->getCombinationAttackCD();

        mask2->setAngle(0,(score2/150.0)*360*16);
        qDebug()<<"score2"<<score2;
    }


//    update();
//    mask1->setAngle(0,score1*16);
//    mask2->setAngle(0,score2*16);
//    qDebug()<<"score1"<<score1<<"score2"<<score2;

    //p1技能点
    if(p1->getSkillPoint()==0){
        p1->getpointLabel1()->setPixmap(QPixmap(":/point1.png"));
        p1->getpointLabel2()->setPixmap(QPixmap(":/point1.png"));
        p1->getpointLabel3()->setPixmap(QPixmap(":/point1.png"));
    }
    else if(p1->getSkillPoint()==1){
        p1->getpointLabel1()->setPixmap(QPixmap(":/point2.png"));
    }else if(p1->getSkillPoint()==2){
        p1->getpointLabel1()->setPixmap(QPixmap(":/point2.png"));
        p1->getpointLabel2()->setPixmap(QPixmap(":/point2.png"));
    }else if(p1->getSkillPoint()==3){
        p1->getpointLabel1()->setPixmap(QPixmap(":/point2.png"));
        p1->getpointLabel2()->setPixmap(QPixmap(":/point2.png"));
        p1->getpointLabel3()->setPixmap(QPixmap(":/point2.png"));
    }

    //p2技能点
    if(p2->getSkillPoint()==0){
        p2->getpointLabel1()->setPixmap(QPixmap(":/point1.png"));
        p2->getpointLabel2()->setPixmap(QPixmap(":/point1.png"));
        p2->getpointLabel3()->setPixmap(QPixmap(":/point1.png"));
    }
    else if(p2->getSkillPoint()==1){
        p2->getpointLabel1()->setPixmap(QPixmap(":/point2.png"));
    }else if(p2->getSkillPoint()==2){
        p2->getpointLabel1()->setPixmap(QPixmap(":/point2.png"));
        p2->getpointLabel2()->setPixmap(QPixmap(":/point2.png"));
    }else if(p2->getSkillPoint()==3){
        p2->getpointLabel1()->setPixmap(QPixmap(":/point2.png"));
        p2->getpointLabel2()->setPixmap(QPixmap(":/point2.png"));
        p2->getpointLabel3()->setPixmap(QPixmap(":/point2.png"));
    }


    if(mode==PVE_MODE){
        aien->run(p1);
    }
    scoreLabel1->setText(QString::number(p1->getScore()));
    scoreLabel2->setText(QString::number(p2->getScore()));

    b2Vec2 p1Pos =p1->getBody()->GetPosition();
    b2Vec2 p2Pos =p2->getBody()->GetPosition();
    b2Vec2 p1p1 = p1Pos + b2Vec2(0, 0);
    b2Vec2 p2p1 = p2Pos + b2Vec2(0, 0);
    b2Vec2 p1dir=p1->getDirection();
    b2Vec2 p2dir=p2->getDirection();
    b2Vec2 p1p2 = p1Pos + b2Vec2(p1dir.x*8,p1dir.y*8);
    b2Vec2 p2p2 = p2Pos + b2Vec2(p2dir.x*8,p2dir.y*8);
    b2RayCastInput input1;
    input1.p1 = p1p1;  //起始位置
    input1.p2 = p1p2;
    input1.maxFraction = 1.0f;
    b2RayCastOutput output1;
    bool isEnter1=p2->getBody()->GetFixtureList()->RayCast(&output1,input1,0);
    p1->setAreaActive(isEnter1);
    b2RayCastInput input2;
    input2.p1 = p2p1;  //起始位置
    input2.p2 = p2p2;
    input2.maxFraction = 1.0f;
    b2RayCastOutput output2;
    bool isEnter2=p1->getBody()->GetFixtureList()->RayCast(&output2,input2,0);
     p2->setAreaActive(isEnter2);
}


//按键触发的keyPressEvent事件，isAutoRepeat返回false；自动触发的keyPressEvent事件，isAutoRepeat返回true；
//松键触发的keyReleaseEvent事件，isAutoRepeat返回true；自动触发的keyReleaseEvent事件，isAutoRepeat返回false。

void CombatScene::keyPressEvent(QKeyEvent *event){
    //p1
            PlayerStatus prestatus=p1->getStatus();
            if(event->key()==Qt::Key_J&&!event->isAutoRepeat()){
                if(prestatus==moveRight||prestatus==standRight){
                    qDebug()<<"向右攻击";
//                    keyPressFlag= true;
                    p1->setStatus(attackRight);
                }
                if(prestatus==moveLeft||prestatus==standLeft){
                    qDebug()<<"向左攻击";
//                     keyPressFlag= true;
                    p1->setStatus(attackLeft);
                }
            }else
            if(event->key()==Qt::Key_K&&!event->isAutoRepeat()){
                if(prestatus==moveRight||prestatus==standRight){
                    qDebug()<<"向右释放技能";
//                     keyPressFlag= true;
                    p1->setStatus(combinationAttackRight);
                }
                if(prestatus==moveLeft||prestatus==standLeft){
                    qDebug()<<"向左释放技能";
//                     keyPressFlag= true;
                    p1->setStatus(combinationAttackLeft);
                }
            }else
            if(event->key()==Qt::Key_S&&!event->isAutoRepeat()){
                if(prestatus==moveRight||prestatus==standRight){
                    qDebug()<<"向右格挡";
//                     keyPressFlag= true;
                    p1->setStatus(defenseRight);
                }
                if(prestatus==moveLeft||prestatus==standLeft){
                    qDebug()<<"向左格挡";
//                     keyPressFlag= true;
                    p1->setStatus(defenseLeft);
                }
            }



            if(p1->getStatus()!=jumpingLeft&&p1->getStatus()!=jumpingRight){
                if(event->key()==Qt::Key_W&&!event->isAutoRepeat()) {
                    qDebug()<<"跳跃";
//                    keyPressFlag= true;
                    PlayerStatus preStatus=p1->getStatus();
                    if(preStatus==moveRight){
                        p1->setStatus(jumpRight);
                    }else if(preStatus==moveLeft){
                        p1->setStatus(jumpLeft);
                    }else if(preStatus==standRight){
                        p1->setStatus(jumpUpRight);
                    }else if(preStatus==standLeft){
                        p1->setStatus(jumpUpLeft);
                    }
                }
                else if(event->key()==Qt::Key_A&&p1->getStatus()!=moveLeft){
                    if(p1->getStatus()==standRight||p1->getStatus()==standLeft){
                        qDebug()<<"向左移动";
//                        keyPressFlag= true;
                        p1->setStatus(moveLeft);
                    }
                }
                else if(event->key()==Qt::Key_D&&p1->getStatus()!=moveRight){
                    if(p1->getStatus()==standRight||p1->getStatus()==standLeft){
                        qDebug()<<"向右 移动";
//                        keyPressFlag= true;
                        p1->setStatus(moveRight);
                    }
                }

            }

            if(mode==PVP_MODE){


            PlayerStatus prestatus2=p2->getStatus();
            if(event->key()==Qt::Key_1&&!event->isAutoRepeat()){
                if(prestatus2==moveRight||prestatus2==standRight){
                    qDebug()<<"p2向右攻击";
//                     keyPressFlag2= true;
                    p2->setStatus(attackRight);
                }
                if(prestatus2==moveLeft||prestatus2==standLeft){
                    qDebug()<<"p2向左攻击";
//                     keyPressFlag2= true;
                    p2->setStatus(attackLeft);
                }
            }else
            if(event->key()==Qt::Key_2&&!event->isAutoRepeat()){
                if(prestatus2==moveRight||prestatus2==standRight){
                    qDebug()<<"p2向右释放技能";
//                     keyPressFlag2= true;
                    p2->setStatus(combinationAttackRight);
                }
                if(prestatus2==moveLeft||prestatus2==standLeft){
                    qDebug()<<"p2向左释放技能";
//                     keyPressFlag2= true;
                    p2->setStatus(combinationAttackLeft);
                }
            }else
            if(event->key()==Qt::Key_Down&&!event->isAutoRepeat()){
                if(prestatus2==moveRight||prestatus2==standRight){
                    qDebug()<<"p2向右格挡";
//                     keyPressFlag2= true;
                    p2->setStatus(defenseRight);
                }
                if(prestatus2==moveLeft||prestatus2==standLeft){
                    qDebug()<<"p2向左格挡";
//                     keyPressFlag2= true;
                    p2->setStatus(defenseLeft);
                }
            }

//            qDebug()<<"p2行动"<<endl;
            if(p2->getStatus()!=jumpingLeft&&p2->getStatus()!=jumpingRight){
                if(event->key()==Qt::Key_Up&&!event->isAutoRepeat()) {
                    qDebug()<<"p2跳跃";
//                    keyPressFlag2= true;
                    PlayerStatus preStatus2=p2->getStatus();
                    if(preStatus2==moveRight){
                        p2->setStatus(jumpRight);
                    }else if(preStatus2==moveLeft){
                        p2->setStatus(jumpLeft);
                    }else if(preStatus2==standRight){
                        p2->setStatus(jumpUpRight);
                    }else if(preStatus2==standLeft){
                        p2->setStatus(jumpUpLeft);
                    }
                }
                else if(event->key()==Qt::Key_Left){
                    if(p2->getStatus()==standRight||p2->getStatus()==standLeft){
                        qDebug()<<"p2向左移动";
//                        keyPressFlag2= true;
                        p2->setStatus(moveLeft);
                    }
                }
                else if(event->key()==Qt::Key_Right){
                    if(p2->getStatus()==standRight||p2->getStatus()==standLeft){
                        qDebug()<<"p2向右 移动";
//                        keyPressFlag2= true;
                        p2->setStatus(moveRight);
                    }
                }

            }
            }
}


void CombatScene::keyReleaseEvent(QKeyEvent *event){
    //p1

        if(event->key()==Qt::Key_W) {
            qDebug()<<"返回jumping";
//            keyPressFlag= false;
            if(p1->getStatus()==jumpRight||p1->getStatus()==jumpUpRight){
                p1->setStatus(jumpingRight);

            }
            if(p1->getStatus()==jumpLeft||p1->getStatus()==jumpUpLeft){
                p1->setStatus(jumpingLeft);
            }

        }
        else if(!event->isAutoRepeat()&&event->key()==Qt::Key_A){
            qDebug()<<"返回站立姿势向左";
//            keyPressFlag= false;
            p1->setStatus(standLeft);
        }
        else if(!event->isAutoRepeat()&&event->key()==Qt::Key_D){
            qDebug()<<"返回站立姿势向右";
//            keyPressFlag= false;
            p1->setStatus(standRight);
        }
//        else if(!event->isAutoRepeat()&&event->key()==Qt::Key_J){
//            if(p1->getStatus()==attckRight){
//                qDebug()<<"向右站立";
//                keyPressFlag= false;
//                p1->setStatus(standRight);
//            }
//            if(p1->getStatus()==attckLeft){
//                qDebug()<<"向左站立";
//                keyPressFlag= false;
//                p1->setStatus(standLeft);
//            }
//        }
//        else if(keyPressFlag&&!event->isAutoRepeat()&&event->key()==Qt::Key_K){
//            if(p1->getStatus()== combinationAttackRight){
//                qDebug()<<"向右站立";
//                keyPressFlag= false;
//                p1->setStatus(standRight);
//            }
//            if(p1->getStatus()==combinationAttackLeft){
//                qDebug()<<"向左站立";
//                keyPressFlag= false;
//                p1->setStatus(standLeft);
//            }
//        }
        else if(!event->isAutoRepeat()&&event->key()==Qt::Key_S){
            if(p1->getStatus()== defenseRight){
                qDebug()<<"向右站立";
//                keyPressFlag= false;
                p1->setStatus(standRight);
            }
            if(p1->getStatus()==defenseLeft){
                qDebug()<<"向左站立";
//                keyPressFlag= false;
                p1->setStatus(standLeft);
            }
        }



    //p2

         if(event->key()==Qt::Key_Up) {
             qDebug()<<"p2返回jumping";
//             keyPressFlag2= false;
             if(p2->getStatus()==jumpRight||p2->getStatus()==jumpUpRight){
                 p2->setStatus(jumpingRight);

             }
             if(p2->getStatus()==jumpLeft||p2->getStatus()==jumpUpLeft){
                 p2->setStatus(jumpingLeft);
             }

         }
         else if(!event->isAutoRepeat()&&event->key()==Qt::Key_Left){
             qDebug()<<"p2返回站立姿势向左";
//             keyPressFlag2= false;
             p2->setStatus(standLeft);
         }
         else if(!event->isAutoRepeat()&&event->key()==Qt::Key_Right){
             qDebug()<<"p2返回站立姿势向右";
//             keyPressFlag2= false;
             p2->setStatus(standRight);
         }
//         else if(keyPressFlag2&&!event->isAutoRepeat()&&event->key()==Qt::Key_1){
//             if(p2->getStatus()==attckRight){
//                 qDebug()<<"p2向右站立";
//                 keyPressFlag2= false;
//                 p2->setStatus(standRight);
//             }
//             if(p2->getStatus()==attckLeft){
//                 qDebug()<<"p2向左站立";
//                 keyPressFlag2= false;
//                 p2->setStatus(standLeft);
//             }
//         }
//         else if(keyPressFlag2&&!event->isAutoRepeat()&&event->key()==Qt::Key_2){
//             if(p2->getStatus()== combinationAttackRight){
//                 qDebug()<<"p2向右站立";
//                 keyPressFlag2= false;
//                 p2->setStatus(standRight);
//             }
//             if(p2->getStatus()==combinationAttackLeft){
//                 qDebug()<<"p2向左站立";
//                 keyPressFlag2= false;
//                 p2->setStatus(standLeft);
//             }
//         }
         else if(!event->isAutoRepeat()&&event->key()==Qt::Key_Down){
             if(p2->getStatus()== defenseRight){
                 qDebug()<<"p2向右站立";
//                 keyPressFlag2= false;
                 p2->setStatus(standRight);
             }
             if(p2->getStatus()==defenseLeft){
                 qDebug()<<"p2向左站立";
//                 keyPressFlag2= false;
                 p2->setStatus(standLeft);
             }
         }


}

//玩家落地
void CombatScene::playerInFloor(QString name){
    Player* player;
    if(name=="p1"){
        player=p1;
    }else{
        player=p2;
    }
    if(player->getStatus()==jumpingRight){
        player->setStatus(standRight);
    }else if(player->getStatus()==jumpingLeft){
        player->setStatus(standLeft);

    }
}


//玩家受到普通攻击
void CombatScene::playerIsAttacked(QString name){
    if(name=="p1"){
      p2->setScore(p1->getScore()+10);

    }
    if(name=="p2"){
        p1->setScore(p1->getScore()+10);
    }
}

//受到组合技攻击
void CombatScene::playerIsCombinationAttacked(QString name){
    if(name=="p1"){
      p2->setScore(p1->getScore()+100);
    }
    if(name=="p2"){
        p1->setScore(p1->getScore()+100);
    }
}

void CombatScene::setMode(GameMode mode){
    this->mode=mode;
}







